package com.wukaige.mongo.controller;

import com.wukaige.mongo.config.JwtUtils;
import com.wukaige.mongo.config.ResponseResult;
import com.wukaige.mongo.entity.Cart;
import com.wukaige.mongo.mapper.CartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("cart")
public class CartController {

    @Autowired
    CartMapper cartMapper;

    @RequestMapping("addcart")
    public ResponseResult addcart(@RequestBody Cart cart, HttpServletRequest request) {
        JwtUtils jwtUtils = new JwtUtils();
        String token = request.getHeader("token");
        String userIdByToken = jwtUtils.getUserIdByToken(token);
        cart.setUid(Integer.parseInt(userIdByToken));
        cart.setGnumber(1);
        long count = cartMapper.count();
        int i = Integer.parseInt(String.valueOf(count));

        cart.setCid(i + 1);
        BigDecimal subtotal = BigDecimal.valueOf(cart.getGnumber()).multiply(cart.getPrice());
        cart.setSubtotal(subtotal);
        List<Cart> byUid = cartMapper.findByUid(cart.getUid());
        for (Cart cart1 : byUid) {
            if (cart1.getGid().equals(cart.getGid())) {
                cartMapper.removeByGid(cart1.getGid());
                cart.setGnumber(cart1.getGnumber() + 1);
                cart.setSubtotal(cart.getPrice().multiply(BigDecimal.valueOf(cart.getGnumber())));
            }
        }
        System.out.println(cart);
        Cart save = cartMapper.save(cart);
        return ResponseResult.success(save);
    }

    @RequestMapping("getcart")
    public ResponseResult getcart(HttpServletRequest request) {
        JwtUtils jwtUtils = new JwtUtils();
        String token = request.getHeader("token");
        String userIdByToken = jwtUtils.getUserIdByToken(token);
        List<Cart> carts = cartMapper.findByUid(Integer.parseInt(userIdByToken));

        System.out.println("cartssssssssssssssssssssssssssssss" + carts);
        return ResponseResult.success(carts);
    }

    @RequestMapping("postcart")
    public ResponseResult postcart(@RequestBody Cart cart, HttpServletRequest request) {
        JwtUtils jwtUtils = new JwtUtils();
        String token = request.getHeader("token");
        String userIdByToken = jwtUtils.getUserIdByToken(token);
        List<Cart> byUid = cartMapper.findByUid(Integer.parseInt(userIdByToken));
        for (Cart cart1 : byUid) {
            if (cart.getGid().equals(cart1.getGid()) && cart.getUid().equals(cart1.getUid())) {
                cartMapper.removeByGid(cart1.getGid());
            }
        }
        Cart save = cartMapper.save(cart);
        byUid.add(save);
        return ResponseResult.success(byUid);
    }

    @RequestMapping("delcart")
    public ResponseResult delcart(@RequestBody Cart cart) {
        int i = cartMapper.removeByGid(cart.getGid());
        return ResponseResult.success(i);
    }

    @RequestMapping("delcartbyuid")
    public boolean delcartbyuid(@RequestBody List<Cart> cartlist) {
        for (Cart cart : cartlist) {
            System.out.println(cart);
            int i = cartMapper.removeByCid(cart.getCid());
        }
        return true;
    }
}
